﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace NJL.WebManage.Ad
{
    public partial class AddAd : BasePage
    {
        NJL.BLL.NJL_CS_AD_SPACE bllAdSpage = new BLL.NJL_CS_AD_SPACE();
        NJL.Model.NJL_CS_AD_SPACE modelAdSpace = new Model.NJL_CS_AD_SPACE();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["editid"] == null)
                {
                    rblState.Items.Add(new ListItem("固定广告位", "1"));
                    rblState.Items[0].Selected = true;
                    if (WEBSITE_CODE == "1")
                    {
                        rblState.Items.Add(new ListItem("临时广告位", "2"));
                    }
                    else
                    {
                        ddlType.Items.RemoveAt(0);
                    }
                    DataSet ds = bllAdSpage.GetList(1, "", "ID desc");
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        txtID.Text = (Convert.ToInt32(ds.Tables[0].Rows[0][0]) + 1).ToString();
                    }
                }
                else
                {              
                    Reset1.Attributes.Add("onclick", "window.location.href='AdManage.aspx'");
                    Reset1.Value = "返回";
                    modelAdSpace = bllAdSpage.GetModel(Request.QueryString["editid"]);
                    txtID.Text = modelAdSpace.ID;
                    txtName.Text=modelAdSpace.AD_SPACE_NAME ;
                    ddlType.SelectedValue = modelAdSpace.AD_SPACE_TYPE;
                    txtWidth.Text = modelAdSpace.AD_SPACE_WIDTH.ToString();
                    txtHeight.Text = modelAdSpace.AD_SPACE_HEIGHT.ToString();
                    rblState.Items.Add(new ListItem("固定广告位", "1"));
                    if (WEBSITE_CODE == "1")
                    {
                        rblState.Items.Add(new ListItem("临时广告位", "2"));
                    }
                    rblState.Items.FindByValue(modelAdSpace.AD_SPACE_STATE).Selected = true;
                    txtID.Enabled = false;
                }
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["editid"] == null)
            {
                modelAdSpace.ID = txtID.Text.Trim();
                modelAdSpace.AD_SPACE_NAME = txtName.Text.Trim();
                modelAdSpace.AD_SPACE_WIDTH = Convert.ToInt32(txtWidth.Text.Trim());
                modelAdSpace.AD_SPACE_HEIGHT = Convert.ToInt32(txtHeight.Text.Trim());
                modelAdSpace.AD_SPACE_TYPE = ddlType.SelectedValue;
                modelAdSpace.AD_SPACE_STATE = rblState.SelectedValue;
                if (bllAdSpage.Add(modelAdSpace))
                {
                    Royalsoft.Common.MessageBox.ShowAndRedirect(this, "添加成功", Request.RawUrl);
                }
            }
            else
            {
                modelAdSpace = bllAdSpage.GetModel(Request.QueryString["editid"]);
              
                modelAdSpace.AD_SPACE_NAME = txtName.Text.Trim();
                modelAdSpace.AD_SPACE_WIDTH = Convert.ToInt32(txtWidth.Text.Trim());
                modelAdSpace.AD_SPACE_HEIGHT = Convert.ToInt32(txtHeight.Text.Trim());
                modelAdSpace.AD_SPACE_TYPE = ddlType.SelectedValue;
                modelAdSpace.AD_SPACE_STATE = rblState.SelectedValue;
                if (bllAdSpage.Update(modelAdSpace))
                {
                    Royalsoft.Common.MessageBox.ShowAndRedirect(this, "更新成功", Request.RawUrl);
                }
            }
        }
    }
}